package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.List;

import com.app.util.Utils;

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;



// OK
// http://www.cnblogs.com/vtianyun/archive/2012/04/15/2450961.html
// http://www.cnblogs.com/carlos-vic/p/Carlos_V_Android_11.html
public class OnItemClickPlusPos30Activity extends Activity{

	List<Bean>  listdata = new ArrayList<Bean>();
	Adapter adapter ;
	OnItemClickPlusPos30Activity activity = this;
	
	ListView listview;
	
	TextView btn_focus;
	
	Point cur_down_point = new Point();
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_onitemclickpluspos_30);
		
		
		for(int i =0; i<10; i++){
			Bean bean = new Bean();
			bean.d+=i;
			listdata.add(bean);
		}
		
		
		adapter = new Adapter(listdata, activity);
		listview = (ListView)findViewById(R.id.listview);
		listview.setAdapter(adapter);
		
		
		
		
		// listview 点击的区域判断
		listview.setOnItemClickListener( new OnItemClickListener() {

			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Bean  bean =(Bean)parent.getItemAtPosition(position);

				Rect outRect = new Rect();
				parent.getHitRect(outRect);
				
				System.out.println("rect: "+outRect.toString());
				System.out.println("bean: "+bean.d);
				
				
				btn_focus = (TextView)view.findViewById(R.id.btn_focus);
				
				
				Rect r = new Rect();
				btn_focus.getLocalVisibleRect(r);
				System.out.println("getLocalVisibleRect: "+r.toString());
				
				/*
				int[] location = new int[2];
				btn_focus.getLocationInWindow(location);
				System.out.println("getLocationInWindow: "+location[0]+" "+location[1]);
				*/
				
				int[] location1 = new int[2];
				btn_focus.getLocationOnScreen(location1);
				System.out.println("getLocationOnScreen: "+location1[0]+" "+location1[1]);
				
				r.offset(location1[0], location1[1]);
				System.out.println("最终矩形数据: "+r.toString());
				System.out.println("最终点数据: "+cur_down_point.toString());
				
				if(r.contains(cur_down_point.x, cur_down_point.y)){
					System.out.println("在目标区域.");
					Utils.showToastShort(activity, "命中目标");
				}
				
				
				
				
				
			}
		});

		// 在这里记录用户按下的坐标
		listview.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
				float x =event.getRawX();  // .
				float y =event.getRawY();
				System.out.println("X: "+x);  
				System.out.println("Y: "+y);  
				
				cur_down_point.x = (int)x;
				cur_down_point.y = (int)y;
				
				}
				
				return false;
			}
		});
		
		
		
	}
	
	
	
}

class Bean
{
	public String d ="";
	
	
};


class Adapter extends BaseAdapter
{
	
	List<Bean>  listdata;
	Activity activity;
	
	public Adapter(List<Bean> listdata, Activity activity) {
		// TODO Auto-generated constructor stub
		this.listdata = listdata;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listdata.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listdata.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		ViewHold vh = null;
		if(convertView == null || ((ViewHold)convertView.getTag()).pos !=position){
			
			vh = new ViewHold();
			vh.pos = position;
			
			convertView = activity.getLayoutInflater().inflate(
					R.layout.adapter_shops_comment, null);
			
			
			
			
			convertView.setTag(vh);
		}
		else{
			vh = (ViewHold)convertView.getTag();
		}
		
		
		
		return convertView;
	}
	
	
	
	class ViewHold
	{
		
	//	TextView 
		
		public int pos;
	};
	
	
	
	
};


